package com.train.course.controller;

import com.train.common.annotation.Anonymous;
import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.page.TableDataInfo;
import com.train.course.domain.VideoEpisode;
import com.train.course.service.IVideoEpisodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程视频集 控制器
 */
@RestController
@RequestMapping("/course/episode")
public class VideoEpisodeController extends BaseController {
    @Autowired
    private IVideoEpisodeService videoEpisodeService;

    /**
     * 查询视频集列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(VideoEpisode videoEpisode) {
        startPage();
        List<VideoEpisode> list = videoEpisodeService.selectVideoEpisodeList(videoEpisode);
        return getDataTable(list);
    }

    /**
     * 根据课程ID查询视频集列表
     */
    @Anonymous
    @GetMapping("/listByCourse/{courseId}")
    public AjaxResult listByCourse(@PathVariable Long courseId) {
        List<VideoEpisode> list = videoEpisodeService.selectVideoEpisodeListByCourseId(courseId);
        return AjaxResult.success(list);
    }

    /**
     * 获取视频集详细信息
     */
    @Anonymous
    @GetMapping(value = "/{episodeId}")
    public AjaxResult getInfo(@PathVariable("episodeId") Long episodeId) {
        return AjaxResult.success(videoEpisodeService.selectVideoEpisodeById(episodeId));
    }

    /**
     * 新增视频集
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody VideoEpisode videoEpisode) {
        int rows = videoEpisodeService.insertVideoEpisode(videoEpisode);
        if (rows > 0) {
            // MyBatis 会将生成的 episodeId 回填到 videoEpisode 对象中
            Map<String, Object> data = new HashMap<>();
            data.put("episodeId", videoEpisode.getEpisodeId());
            return AjaxResult.success("操作成功", data);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改视频集
     */
    @Anonymous
    @PutMapping
    public AjaxResult edit(@RequestBody VideoEpisode videoEpisode) {
        return toAjax(videoEpisodeService.updateVideoEpisode(videoEpisode));
    }

    /**
     * 删除视频集
     */
    @Anonymous
    @DeleteMapping("/{episodeIds}")
    public AjaxResult remove(@PathVariable Long[] episodeIds) {
        return toAjax(videoEpisodeService.deleteVideoEpisodeByIds(episodeIds));
    }
}